//
//  WKDakaImageTextVC.m
//  V-Learn
//
//  Created by Apple on 2019/3/19.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "WKDakaImageTextVC.h"
#import "WKDakaImageViewCell.h"
#import "WKDakaDetailImageTextCell.h"
//#import "VKRecordInfoModel.h"
#import "WKHotclockInfoVOListModel.h"

@interface WKDakaImageTextVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIImageView *footImageView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *headerImageView;

@end

@implementation WKDakaImageTextVC
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, ScreenW, ScreenH-LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"打卡详情";
    [self setupNavigationBar];
    [self.view addSubview:self.tableView];
//    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 40;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    //尾部视图
    self.footerView = [UIView new];
    self.footImageView = [[UIImageView alloc] init];
    [self.footerView addSubview:self.footImageView];
    self.footerView.frame = CGRectMake(0, 0, ScreenW, 1);
    self.footImageView.frame = CGRectMake(0, 0, ScreenW, 1);
    //头部视图
    self.headerView = [UIView new];
    self.headerImageView = [[UIImageView alloc] init];
    [self.headerView addSubview:self.headerImageView];
    self.headerView.frame = CGRectMake(0, 0, ScreenW, 1);
    self.headerImageView.frame = CGRectMake(0, 0, ScreenW, 1);
    
    //请求数据
    [self requestImagesData];
    
}
#pragma mark - 获取图片接口
- (void)requestImagesData{
    //    ImageConfigController/queryBackImage
    __weak typeof(self)weakSelf = self;
    [WYHXNetWorkManager get:WKQueryBackImage_WKURL params:@{} completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        if ([code integerValue]==200) {
//            NSLog(@"%@",responseObj);
            NSDictionary *dic = responseObj[@"data"];
            NSString *clockUpImage = dic[@"clockUpImage"];
            NSString *clockDownImage = dic[@"clockDownImage"];
            //            NSString *recommendImage = dic[@"recommendImage"];
            NSString *urlStringUpImage = [NSString stringWithFormat:@"%@%@",Public_URL,clockUpImage];
            NSString *urlStringDownImage = [NSString stringWithFormat:@"%@%@",Public_URL,clockDownImage];
            
            [self.footImageView sd_setImageWithURL:[NSURL URLWithString:urlStringUpImage] placeholderImage:[UIImage imageNamed:@"wk_list_bgimg"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

                CGFloat pHeight = 150;
                CGFloat pWidth = image.size.width * pHeight / image.size.height;
                self.footerView.frame = CGRectMake(0, 0, ScreenW, pWidth);
                self.footImageView.frame = CGRectMake(0, 0, ScreenW, pWidth);
                weakSelf.tableView.tableFooterView = self.footerView;
                
            }];
            [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:urlStringDownImage] placeholderImage:[UIImage imageNamed:@"wk_list_bgimg"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

                CGFloat pHeight = 150;
                CGFloat pWidth = image.size.width * pHeight / image.size.height;
                self.headerView.frame = CGRectMake(0, 0, ScreenW,pWidth);
                self.headerImageView.frame = CGRectMake(0, 0, ScreenW, pWidth);
                weakSelf.tableView.tableHeaderView = self.headerView;
                
            }];
        }
        [self.tableView reloadData];
    }];
}
#pragma mark - 设置导航
- (void)setupNavigationBar{
    UIButton *btn = [[UIButton alloc] init];
    [btn addTarget:self action:@selector(clickRightItemBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:LBImage(@"wk_share") forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 30, 30);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.model.clockInfoVOList.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    /**
     WKHotmenModel *model = self.sourceArray[indexPath.section];
     WKHotclockInfoVOListModel
     */
    if (indexPath.section==0) {
        WKDakaImageViewCell *cell = [WKDakaImageViewCell cellWithTableView:tableView];
        cell.titleLab.text = self.model.clockTitle;
        cell.detailLab.text = self.model.clockOrtherTitle;
        return cell;
    }else{
        WKDakaDetailImageTextCell *cell = [WKDakaDetailImageTextCell cellWithTableView:tableView];
        WKHotclockInfoVOListModel *model = self.model.clockInfoVOList[indexPath.section-1];
        cell.model = model;
        return cell;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return UITableViewAutomaticDimension;
    }
//    return 44;
    return UITableViewAutomaticDimension;
}

#pragma mark - 分享按钮点击事件
- (void)clickRightItemBtn:(UIButton *)sender{
    
    [LWShareService shared].shareBtnClickBlock = ^(NSIndexPath *index) {
        NSInteger tag = index.row;
        [[LWShareService shared] hideSheetView];
        NSString *shareUrl;
        if (self.model.clockInfoVOList.count>0) {
            //&userId=227@17630721477@1561520850728&from=singlemessage
            WKHotclockInfoVOListModel *model = self.model.clockInfoVOList[0];
            shareUrl = [NSString stringWithFormat:@"%@%@&userId=%@",WKDakaDetailShare_WKURL,model.clockId,[UserModel getUserModel].token];
            
        }
        switch (tag) {
            case 0:
            {
                //userid 传内部的  在添加一个token 拼接上
                
                WXWebpageObject *webpageObject = [WXWebpageObject object];
                webpageObject.webpageUrl = shareUrl;
                
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = self.model.clockOrtherTitle;
                
                
                if (self.model.clockInfoVOList.count>0) {
                    WKHotclockInfoVOListModel *model2 = self.model.clockInfoVOList[0];
                    message.description = model2.infoContent;
                    
                    message.title = self.model.clockOrtherTitle;
                    if (self.model.clockInfoVOList.count>0) {
                        WKHotclockInfoVOListModel *model2 = self.model.clockInfoVOList[0];
                        message.description = model2.infoContent;
                        [message setThumbImage:[UIImage imageNamed:@"wanke_logo"]];
                        
                    }
                    
                }
                message.mediaObject = webpageObject;
                
                SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = WXSceneSession;
                [WXApi sendReq:req];
            }
                break;
            case 1:
            {
                WXWebpageObject *webpageObject = [WXWebpageObject object];
                webpageObject.webpageUrl = shareUrl;
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = self.model.clockOrtherTitle;
                if (self.model.clockInfoVOList.count>0) {
                    WKHotclockInfoVOListModel *model2 = self.model.clockInfoVOList[0];
                    message.description = model2.infoContent;
                    [message setThumbImage:[UIImage imageNamed:@"wanke_logo"]];
                }
                
                message.mediaObject = webpageObject;
                
                SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = WXSceneTimeline;
                [WXApi sendReq:req];
                
            }
                break;
            default:
                break;
        }
    };
    
    [[LWShareService shared] showInViewController:self];
}
- (NSData *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImageJPEGRepresentation(newImage, 0.8);
}
-(UIImage *) getImageFromURL:(NSString *)fileURL
{
    UIImage * result;
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];
    return result;
    
}
@end
